﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace StoreApp
{
    public class MsqlManager:IDbManger
    {
        const string ConnectionString = "Database=bookstore;Data Source=localhost;User Id=user;Password=password";
        private MySqlConnection m_connection;

        public MsqlManager()
        {
            m_connection = getConnection();
        }

        public bool Insert(int cash)
        {
            String sqlQuery = buildInsertString(cash / 31);
            MySqlCommand command = new MySqlCommand(sqlQuery, m_connection);
            command.ExecuteNonQuery();
            return true;
        }

        private MySqlConnection getConnection()
        {
            var connection = new MySqlConnection(ConnectionString);
            connection.Open();
            return connection;
        }

        protected String buildInsertString(double dollars)
        {
            String sql = (new StringBuilder("insert into ")).Append("cash").Append(" (").ToString();
            sql += (new StringBuilder(sql)).Append("year,version_num,operatins,summ").ToString();
            sql += (new StringBuilder(sql)).Append(") values ('").ToString();
            sql += (new StringBuilder(sql)).Append("2012").Append("',").ToString();
            sql += (new StringBuilder(sql)).Append("325435").Append("',").ToString();
            sql += (new StringBuilder(sql)).Append("cash").Append("',").ToString();
            sql += (new StringBuilder(sql)).Append(dbl2str(dollars)).Append("',").ToString();
            sql += (new StringBuilder(sql)).Append("0,'Clarity','','')").ToString();
            return sql;
        }

        private static String dbl2str(Object obj)
        {
            string value;
            string str = obj.ToString();
            if (str.IndexOf('E') != -1)
            {
                //System.out.print("1  ");
                string[] qwe = str.Split('E');
                string newstr = "";
                int step = int.Parse(qwe[1]);
                for (int ind = 0; ind < qwe[0].Length; ind++)
                {
                    if (step > 0)
                    {
                        if ((ind == (step + 2)) && (ind != qwe[0].Length))
                        {
                            newstr = newstr + '.';
                        }

                        if (str[ind] != '.')
                        {
                            newstr = newstr + str[ind];
                        }
                        if ((ind == qwe[0].Length - 1) && (ind <= step))
                        {
                            for (int ind2 = 0; ind2 < (step + 1 - ind); ind2++)
                            {
                                newstr = newstr + "0";
                            }
                        }
                    }
                    else
                    {
                        if (ind == 0)
                        {
                            newstr = newstr + "0.";
                            for (int ind3 = 0; ind3 > step + 1; ind3--)
                            {
                                newstr = newstr + "0";
                            }
                        }
                        if (str[ind] != '.')
                        {
                            newstr = newstr + str[ind];
                        }
                    }
                }
                value = newstr;
            }
            else
            {
                value = obj.ToString();
            }
            return value;
        }
    }
}
